#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 crane <crane@gosun>
#
# Distributed under terms of the MIT license.

"""
场景1: 两个排序array合并后的可能性.
场景2: 两个并发操作集在total-order视图上的可能性(thread1/thread2, instance1/instance2).
场景3: 两个偏序到total-order, total-order结果的可能性.
"""

import math

def cnt_direct(n, m):
    little = min(n, m)
    great = max(n, m)

    total_out_of_order = 1
    for i in range(little):
        total_out_of_order *= great + i + 1

    out_of_order = math.factorial(little)

    return total_out_of_order / out_of_order

def cnt_rec(n, m):
    if n == 0 or m == 0:
        return 1

    if n == 1: return m + 1
    if m == 1: return n + 1

    cnt = 0
    for first in range(m+1):
        cnt += cnt_rec(n-1, first)

    return cnt

def test_do(m, n):
    s1 = cnt_direct(m, n)
    print("count of: (%s, %s) ===> %s"% (m, n, s1))

    # recursion计算的比较慢
    s1 = cnt_rec(m, n)
    print("count of: (%s, %s) ===> %s"% (m, n, s1))

def test():
    test_do(2, 2)
    test_do(3, 3)
    test_do(3, 3)
    test_do(4, 4)
    test_do(20, 20)

def main():
    print("start main")
    test()

if __name__ == "__main__":
    main()
